package com.wcar.vipmanagement.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.vipmanagement.dao.*;
import com.wcar.vipmanagement.feign.UserFeign;
import com.wcar.vipmanagement.service.AddressService;
import com.wcar.vipmanagement.vo.AddressVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class AddressServiceImpl implements AddressService {
    @Resource
    private AddressDao addressDao;
    @Autowired
    private FrontUserMapper frontUserMapper;
    @Autowired
    private FrontUserStoreDao frontUserStoreDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private StoreUserDao storeUserDao;
    @Override
    public Result<IPage<UserAddress>> getAddressList(AddressVo addressVo) {
        //获取token
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User user = JSON.parseObject(s, User.class);
        QueryWrapper<StoreUser> storeUserWrapper = new QueryWrapper<>();
        if(user.getUserId()!=1){
            storeUserWrapper.eq("user_id",user.getUserId());
        }
        List<StoreUser> storeUsers = storeUserDao.selectList(storeUserWrapper);
        ArrayList<Integer> userIds = new ArrayList<>();
        for (StoreUser storeUser : storeUsers) {
            userIds.add(storeUser.getStoreId());
        }
        QueryWrapper<FrontUserStore> frontUserWrapper = new QueryWrapper<>();
        if(userIds.size()==0){
            return new Result<>(201,"查询数据成功",null);
        }
        frontUserWrapper.in("store_id",userIds);
        List<FrontUserStore> frontUserStores = frontUserStoreDao.selectList(frontUserWrapper);
        ArrayList<Integer> storeIds = new ArrayList<>();
        for (FrontUserStore frontUserStore : frontUserStores) {
            storeIds.add(frontUserStore.getUserId());
        }
        IPage<UserAddress> page = new Page<>(addressVo.getCurrentPage(),addressVo.getPageSize());
        QueryWrapper<UserAddress> wrapper = new QueryWrapper<>();
        if(addressVo.getAddressName()!=null&&addressVo.getAddressName()!=""){
            wrapper.like("address_name",addressVo.getAddressName());
        }
        wrapper.in("user_id",storeIds);
        IPage<UserAddress> list = addressDao.selectPage(page,wrapper);
        List<UserAddress> records = list.getRecords();
        for (UserAddress storeAddress:records){
            storeAddress.setUserName(frontUserMapper.selectById(storeAddress.getUserId()).getUserName());
        }
        return new Result<>(200,"查询数据成功",list);
    }

    @Override
    public Result<String> updateAddress(UserAddress storeAddress) {
        addressDao.updateById(storeAddress);
        return new Result<>(200,"修改信息成功",null);
    }
}
